package com.ym.b2b.user.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.ym.b2b.commons.exception.BaseException;
import com.ym.b2b.commons.service.BaseService;
import com.ym.b2b.user.dao.UserFavoritesDao;
import com.ym.b2b.user.service.UserFavoritesService;
import com.ym.b2b.user.sqlbean.EmSUserFavorites;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;

@Service
public class UserFavoritesServiceImpl extends BaseService implements UserFavoritesService {

    @Autowired
    private UserFavoritesDao userFavoritesDao;

    @Override
    public Page<EmSUserFavorites> searchPageUserProductFavorites(EmSUserFavorites emSUserFavorites,
                                                                 Page<EmSUserFavorites> page) throws BaseException {
        return userFavoritesDao.searchPageUserProductFavorites(emSUserFavorites, page);
    }

    @Override
    public Long searchUserProductFavoritesNum(EmSUserFavorites emSUserFavorites) throws BaseException {
        return userFavoritesDao.searchUserProductFavoritesNum(emSUserFavorites);
    }

    @Override
    public Result deleteUserFavorites(Long favoritesId) throws BaseException {
        Result result = new Result();
        try {
            userFavoritesDao.deleteUserFavorites(favoritesId);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result.setCodeResult(Result.CODE_100, "删除收藏失败");
        }
        return result;
    }

    @Override
    public Result addUserFavorites(EmSUserFavorites emSUserFavorites) throws DataAccessException {
        Result result = new Result();
        try {
        	EmSUserFavorites favorites = new EmSUserFavorites();
        	favorites.setObjectName(emSUserFavorites.getObjectName());
        	favorites.setUserId(emSUserFavorites.getUserId());
        	long count = userFavoritesDao.searchUserProductFavoritesNum(favorites);
        	if(count >0){
        		result.setCodeResult(Result.CODE_100, "已经收藏过此产品 ！");
        	}else{
        		userFavoritesDao.addUserFavorites(emSUserFavorites);
        	}
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result.setCodeResult(Result.CODE_100, "加入收藏失败!");
        }
        return result;
    }

}
